#include <stdio.h>
#include "mysqlpool.h"



int initMysqlClient(MYSQL** mysql){
    MYSQL *ab;
    ab = mysql_init(NULL);

    if(NULL == mysql_real_connect(ab,"localhost","test","","mysql",3306,NULL,0)){
        printf(mysql_error(ab));
        return 0;
    }
    *mysql = ab;


    return 1;
}

mysql_client_queue *getFreeQueue(){
    pthread_mutex_lock (&mutex);
    mysql_client_queue *temp;
    temp = MYSQL_client_queue;
    while(temp!=NULL){
        if(temp->use == 0){
            temp->use = 1;
            pthread_mutex_unlock (&mutex);
            return temp;
        }
        temp = temp->next;
    }
    pthread_mutex_unlock (&mutex);
    return temp;
}

void freeQueue(mysql_client_queue *queue){
    pthread_mutex_lock (&mutex);
    queue->use = 0;
    pthread_mutex_unlock (&mutex);
}

mysql_client_queue* initClientQueue(){
    mysql_client_queue *temp;
    MYSQL *temp_mysql;
    temp=(mysql_client_queue *)malloc(sizeof(mysql_client_queue));
    temp->use = 0;
    temp->key = _number;
    _number++;
    initMysqlClient(&temp_mysql);
    temp->client = temp_mysql;
    return temp;
}

mysql_client_queue* initClientQueues(int number){
    mysql_client_queue *first,*temp;

    first = initClientQueue();

    temp = first;

    for (int i = 0; i <number-1 ; i++) {
        temp->next = initClientQueue();
        temp = temp->next;
    }

    return first;
}

void freeMysql(){
    free(MYSQL_client_queue);
}


void getRet(MYSQL *t_mysql, char *select){
    if(mysql_query(t_mysql,select))
    {
        printf(mysql_error(t_mysql));
        return ;
    }

    MYSQL_RES * result = mysql_store_result(t_mysql);
    MYSQL_ROW row;
    while(row = mysql_fetch_row(result)){
        //printf("%s %s\n",row[1],row[2]);
    }

    LOG("getRet:get ret success!\n");

    mysql_free_result(result);
}




void threadReadyMysql(){
    MYSQL *t_mysql;
    mysql_client_queue *t_queue;

    char select[] = "select * from user ";

    for(int i =1;i<=10000;i++){
        t_queue = getFreeQueue();
        if(t_queue == NULL){
            LOG("no free queue");
            return ;
        }

        LOG("use free queue:%d   ",t_queue->key);

        t_mysql = t_queue->client;

        if(NULL == t_mysql){
            printf("init failed\n");
            return ;
        }

        getRet(t_mysql,select);
        freeQueue(t_queue);
    }

}

int main() {
    MYSQL_client_queue = initClientQueues(100);

    pthread_t ids[10];
    int i,ret;
    for(i = 0;i<=9;i++){
        ret=pthread_create(&ids[i],NULL,(void *) threadReadyMysql,NULL); // 成功返回0，错误返回错误编号
        if(ret!=0) {
            printf ("Create pthread error!\n");
            exit (1);
        }
    }



    pthread_exit(NULL);//等各个线程退出后，进程才结束，否则进程强制结束了，线程可能还没反应过来；
    return 1;

    //

    MYSQL *t_mysql;
    mysql_client_queue *t_queue;

    char select[] = "select * from user ";

    for(int i =1;i<=10;i++){
        t_queue = getFreeQueue();
        if(t_queue == NULL){
            LOG("no free queue");
            return 0;
        }

        LOG("use free queue:%d   ",t_queue->key);

        t_mysql = t_queue->client;

        if(NULL == t_mysql){
            printf("init failed\n");
            return 0;
        }
        getRet(t_mysql,select);
        freeQueue(t_queue);
    }


    freeMysql();

    printf("Hello, World!\n");
    return 0;
}